<?php
namespace App\Controllers\Sys;

use App\Controllers\BaseController;
use App\Library\Helper;
use App\Models\SysUser;
use App\Models\SysRole;

class UserController extends BaseController {

    public function initialize() {
        parent::initialize();
    }

    //----------------------------------------------------------------
    // 系统用户信息列表
    //----------------------------------------------------------------
    public function indexAction() {
        $this->display('sys/user/index');
    }

    //----------------------------------------------------------------
    // 获取系统用户基本信息
    //----------------------------------------------------------------
    public function getsAction() {
        $data = [
            'page' => $this->request->getPost('page') != '' ? $this->request->getPost('page') : $this->request->getQuery('page'),
            'size' => $this->request->getPost('size') != '' ? $this->request->getPost('size') : $this->request->getQuery('size')
        ];

        if($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
        }
        if($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页显示的条数！');
        }
        $data['page'] = intval($data['page']);
        $data['size'] = intval($data['size']);

        $offset = intval(($data['page'] - 1) * $data['size']);

        $user = new SysUser();
        $ret  = $user->gets($offset, $data['size']);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        }
        return $this->sendAjax(false, "获取数据失败，系统错误！");
    }

    //----------------------------------------------------------------
    // 统计系统用户基本信息的总记录数
    //----------------------------------------------------------------
    public function countAction() {
        $user = new SysUser();
        $ret  = $user->total();
        if($ret !== false && is_integer($ret)) {
            return $this->sendAjax(true, $ret);
        }
        return $this->sendAjax(false, '获取总数失败，系统错误！');
    }

    //----------------------------------------------------------------
    // 新增系统用户基本信息
    //----------------------------------------------------------------
    public function addAction() {
        if($this->request->isGet()) {
            $this->display('sys/user/add');
        } else {
            $data = [
                'account'        => trim($this->request->getPost('account')),
                'role_id'        => $this->request->getPost('role_id'),
                'name'           => trim($this->request->getPost('name')),
                'password'       => trim($this->request->getPost('password')),
                'status'         => $this->request->getPost('status'),
                'create_at'      => Helper::getNow(),
                'last_update_at' => Helper::getNow()
            ];

            if($data['name'] == '') {
                return $this->sendAjax(false, '新增运营人员失败，姓名不能为空！');
            }
            if($data['role_id'] == '0' || !is_numeric($data['role_id']) || $data['role_id'] < 1) {
                return $this->sendAjax(false, '新增运营人员失败，请选择角色！');
            }
            if($data['account'] == '') {
                return $this->sendAjax(false, '新增运营人员失败，账户名不能为空！');
            }
            if($data['password'] == '') {
                return $this->sendAjax(false, '新增运营人员失败，密码不能为空！');
            }
            $data['password'] = md5($data['password']);

            $user            = new SysUser();
            $data['account'] = trim($data['account']);
            $checker         = $user->getByAccount($data['account']);
            if(is_array($checker) && count($checker) > 1) {
                return $this->sendAjax(false, '新增运营人员失败，已经添加了相同账户的管理人员！');
            }
            $ret = $user->add($data);
            if($ret) {
                return $this->sendAjax(true, '新增运营人员成功');
            } else {
                return $this->sendAjax(false, '新增运营人员失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 修改系统用户的状态
    //----------------------------------------------------------------
    public function changeStatusAction() {
        $id    = $this->request->getPost('id') != '' ? $this->request->getPost('id') : $this->request->getQuery('id');
        $user  = new SysUser();
        $state = $user->changeStatus($id);
        if($state) {
            return $this->sendAjax(true, '改变状态成功');
        } else {
            return $this->sendAjax(false, '改变状态失败，系统错误！');
        }
    }

    //----------------------------------------------------------------
    // 获取所有的角色
    //----------------------------------------------------------------
    public function getRolesAction() {
        $role  = new SysRole();
        $roles = $role->gets();
        $ret   = [];
        foreach($roles as $k => $v) {
            $ret[$k]['id']   = $v['id'];
            $ret[$k]['name'] = $v['name'];
        }
        if($ret) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, '获取角色失败，系统错误！');
        }
    }
}
